package com.yutian.example.demo.common.utils;

import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JsonUtils {
    private static final Gson gson = new GsonBuilder().create();

    private static final Gson gsonPretty = new GsonBuilder().setPrettyPrinting().create();

    public static <T> T fromJsonToClass(String dataJson, Class<? extends T> z){
        return gson.fromJson(dataJson, z);
    }

    public static boolean isJsonArray(String dataJson){
        if(StringUtils.isBlank(dataJson)){
            return false;
        }
        return dataJson.startsWith("[") && dataJson.endsWith("]");
    }

    public static boolean isJson(String dataJson){
        if(StringUtils.isBlank(dataJson)){
            return false;
        }
        return dataJson.startsWith("{") && dataJson.endsWith("}");
    }

    /**
     *
     * @param dataJson
     * @return
     */
    public static<T> T fromJson(String dataJson, Class<T> z){
        if(StringUtils.isBlank(dataJson)){
            return null;
        }
        return gson.fromJson(dataJson, z);
    }

    /**
     * 注：Object无法转为String
     * @param dataJson
     * @return
     */
    @Deprecated
    public static Map<String, String> fromJsonToMapStr(String dataJson){
        Type type = new TypeToken<HashMap<String, String>>() {
        }.getType();
        Map<String, String> map = gson.fromJson(dataJson, type);
        return map;
    }

    /**
     * 从json转到对象
     * 注：若json某个字段是Date类型，则无法使用gson.fromJson转换；数值类型，会转为Double！
     * @param json
     * @return
     */
    @Deprecated
    public static Map<String, Object> fromJsonToMap(String json) {
        return gson.fromJson(json, new TypeToken<Map<String, Object>>(){
        }.getType());
    }

    public static List<Map<String, Object>> fromJsonToListMapObj(String dataJson){
        Type type = new TypeToken<List<Map<String, Object>>>() {
        }.getType();
        List<Map<String, Object>> listObj = gson.fromJson(dataJson, type);
        return listObj;
    }

    public static <T> List<T> fromJsonArray(String json, Type tp) {
        if(StringUtils.isBlank(json)){
            return null;
        }
        //Json的解析类对象
        JsonParser parser = new JsonParser();
        //将JSON的String 转成一个JsonArray对象
        JsonArray jsonArray = parser.parse(json).getAsJsonArray();

        List<T> beanList = new ArrayList<>();

        //加强for循环遍历JsonArray
        for (JsonElement item : jsonArray) {
            //使用GSON，直接转成Bean对象
            T bean = gson.fromJson(item, tp);
            beanList.add(bean);
        }
        return beanList;
    }

    public static List<Map<String, Object>> fromJsonArray(Object detail) {
        //Json的解析类对象
        List<Map<String, Object>> dataSet = new ArrayList<Map<String, Object>>();
        if (detail instanceof String) {
            dataSet = fromJsonArray(detail.toString(), new TypeToken<Map<String, Object>>(){
            }.getType());// gson
        } else {
            List<Object> detailList = (List<Object>) detail;
            for (Object obj : detailList) {
                Map<String, Object> t = null;
                if (obj instanceof String) {
                    t = fromJsonToMap(obj.toString());
                } else {
                    t = (Map<String, Object>) obj;
                }
                dataSet.add(t);
            }
        }
        return dataSet;
    }

    /**
     * 注：Object无法转为String
     * @param json
     * @return
     */
    public static List<Map<String, String>> fromJsonToListMapStr(String json){
        Type type = new TypeToken<List<Map<String, String>>>() {
        }.getType();
        List<Map<String, String>> listObj = gson.fromJson(json, type);
        return listObj;
    }

    public static String toJson(Object data) {
        if(null == data){
            return null;
        }
        return gson.toJson(data);
    }

    public static String toJsonPretty(Object data) {
        return gsonPretty.toJson(data);
    }
}
